package com.powernode.controller;

import com.github.pagehelper.Page;
import com.powernode.common.Constants;
import com.powernode.common.Result;
import com.powernode.domain.User;
import com.powernode.domain.VisitLog;
import com.powernode.dto.VisitLogDto;
import com.powernode.service.VisitLogService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 描述：处理拜访日志
 */
@RestController
@RequestMapping("visitLog")
public class VisitLogController {

    @Resource
    private VisitLogService visitLogService;

    //分页查询
    @GetMapping("query.action")
    public Result query(VisitLogDto visitLogDto) {
        Page<VisitLog> page = visitLogService.queryForPage(visitLogDto);
        return new Result(0,"查询成功",page.getTotal(),page.getResult());
    }

    //添加
    @PostMapping("add.action")
    public Result add(VisitLog visitLog, HttpSession session) {
        //设置业务员的ID和名字
        User user = (User) session.getAttribute(Constants.SESSION_USER_KEY);
        visitLog.setUserId(user.getId());
        visitLog.setRealname(user.getRealname());
        try {
            visitLogService.insertSelective(visitLog);
            return new Result(200,"添加成功");
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(-1,"添加失败");
        }
    }



}

